import { Conf } from 'electron-conf/renderer'
export interface IData {
  data: any
}

export default () => {
  const conf = new Conf()
  async function set(key: string, data: any): Promise<void> {
    let cache: IData = { data }
    await conf.set(key, cache)
    localStorage.setItem(key, JSON.stringify(cache))
  }
  async function get(key: string, defaultValue: any = null): Promise<any> {
    const cacheStore = (await conf.get(key)) as IData
    return cacheStore ? cacheStore.data : defaultValue
  }
  async function remove(key: string) {
    await conf.delete(key)
  }
  return { set, get, remove }
}
